home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / sys / os2 / os2pm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-25  |  8.8 KB  |  358 lines

  1. /* $Id: os2pm.c,v 1.1 1994/05/25 09:27:01 mjl Exp $
  2.  * $Log: os2pm.c,v $
  3.  * Revision 1.1  1994/05/25  09:27:01  mjl
  4.  * Decided the os2/ subdir of sys/ was a better place for this.
  5.  *
  6.  * Revision 1.9  1994/03/23  06:34:33  mjl
  7.  * All drivers: cleaned up by eliminating extraneous includes (stdio.h and
  8.  * stdlib.h now included automatically by plplotP.h), extraneous clears
  9.  * of pls->fileset, pls->page, and pls->OutFile = NULL (now handled in
  10.  * driver interface or driver initialization as appropriate).  Special
  11.  * handling for malloc includes eliminated (no longer needed) and malloc
  12.  * prototypes fixed as necessary.
  13.  *
  14.  * Revision 1.8  1993/07/31  07:56:38  mjl
  15.  * Several driver functions consolidated, for all drivers.  The width and color
  16.  * commands are now part of a more general "state" command.  The text and
  17.  * graph commands used for switching between modes is now handled by the
  18.  * escape function (very few drivers require it).  The device-specific PLDev
  19.  * structure is now malloc'ed for each driver that requires it, and freed when
  20.  * the stream is terminated.
  21.  *
  22.  * Revision 1.7  1993/07/01  21:59:41  mjl
  23.  * Changed all plplot source files to include plplotP.h (private) rather than
  24.  * plplot.h.  Rationalized namespace -- all externally-visible plplot functions
  25.  * now start with "pl"; device driver functions start with "plD_".
  26. */
  27.  
  28. /*
  29.     os2pm.c
  30.  
  31.     Geoffrey Furnish
  32.     9-22-91
  33.     
  34.     This driver sends plplot commands to the OS/2 PM PLPLOT Server.
  35.  
  36.     25 March 1992
  37.     VERSION 1.0
  38. */
  39.  
  40. #include "plplotP.h"
  41. #include "drivers.h"
  42. #include "pmdefs.h"
  43.  
  44. #define INCL_BASE
  45. #include <os2.h>
  46.  
  47. /* top level declarations */
  48.  
  49. static USHORT    rv;
  50. static HFILE    hf;
  51. static short    cnt;
  52.  
  53. static PLINT    xold = UNDEFINED;
  54. static PLINT    yold = UNDEFINED;
  55.       
  56. typedef    PLINT    COMMAND_ID;
  57. typedef PLINT * CPARAMS;
  58.  
  59. static void    write_command( COMMAND_ID cid, CPARAMS p );
  60.  
  61. /*----------------------------------------------------------------------*\
  62. *  os2_setup()
  63. *
  64. * Set up device.
  65. \*----------------------------------------------------------------------*/
  66.  
  67. void os2_setup( PLStream *pls )
  68. {
  69. }
  70.  
  71. /*----------------------------------------------------------------------*\
  72. *  os2_orient()
  73. *
  74. * Set up orientation.
  75. \*----------------------------------------------------------------------*/
  76.  
  77. void os2_orient( PLStream *pls )
  78. {
  79. }
  80.  
  81. /*----------------------------------------------------------------------*\
  82. *  plD_init_os2()
  83. *
  84. * Initialize device.
  85. \*----------------------------------------------------------------------*/
  86.  
  87. void    plD_init_os2( PLStream *pls )
  88. {
  89.     USHORT    usAction;
  90.     UCHAR    c = (UCHAR) INITIALIZE;
  91.  
  92.     pls->termin =- 0;        /* not an interactive terminal */
  93.     pls->icol0 = 1;
  94.     pls->width = 1;
  95.     pls->bytecnt = 0;
  96.     pls->page = 1;
  97.  
  98.     if (!pls->colorset)
  99.         pls->color = 1;
  100.  
  101.     plP_setpxl( (PLFLT) PIXEL_RES_X, (PLFLT) PIXEL_RES_Y );
  102.     plP_setphy( 0, PLMETA_X, 0, PLMETA_Y );
  103.  
  104.     rv = DosOpen( PIPE_NAME,        // name of the pipe.
  105.         &hf,            // address of file handle.
  106.         &usAction,        // action taken.
  107.         0L,            // size of file.
  108.         FILE_NORMAL,        // file attribute.
  109.         FILE_OPEN,        // open the file.
  110.         OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE,
  111.         0L );
  112.     if (rv)
  113.     plexit( "Unable to open connection to PM server.\n" );
  114.     
  115.     write_command( c, NULL );
  116. }
  117.  
  118. /*----------------------------------------------------------------------*\
  119. *  plD_line_os2()
  120. *
  121. * Draw a line in the current color from (x1,y1) to (x2,y2).
  122. \*----------------------------------------------------------------------*/
  123.  
  124. void plD_line_os2( PLStream *pls, 
  125.             short x1, short y1, short x2, short y2 )
  126. {
  127.     UCHAR c;
  128.     PLINT    cp[4];
  129.  
  130.     if (    x1 < 0 || x1 > PLMETA_X ||
  131.         x2 < 0 || x2 > PLMETA_X ||
  132.         y1 < 0 || y1 > PLMETA_Y ||
  133.         y2 < 0 || y2 > PLMETA_Y     ) {
  134.         printf( "Something is out of bounds." );
  135.     }
  136.  
  137. /* If continuation of previous line send the LINETO command, which uses 
  138.    the previous (x,y) point as it's starting location.  This results in a
  139.    storage reduction of not quite 50%, since the instruction length for
  140.    a LINETO is 5/9 of that for the LINE command, and given that most 
  141.    graphics applications use this command heavily.
  142.  
  143.    Still not quite as efficient as tektronix format since we also send the
  144.    command each time (so shortest command is 25% larger), but a heck of
  145.    a lot easier to implement than the tek method.
  146. */
  147.     if(x1 == xold && y1 == yold) {
  148.  
  149.         c = (UCHAR) LINETO;
  150.         cp[0]=x2;
  151.         cp[1]=y2;
  152.         write_command( c, cp );
  153.     }
  154.     else {
  155.         c = (UCHAR) LINE;
  156.         cp[0] = x1;
  157.         cp[1] = y1;
  158.         cp[2] = x2;
  159.         cp[3] = y2;
  160.         write_command( c, cp );
  161.     }
  162.     xold = x2;
  163.     yold = y2;
  164. }
  165.   
  166. /*----------------------------------------------------------------------*\
  167. * plD_polyline_os2()
  168. *
  169. * Draw a polyline in the current color.
  170. \*----------------------------------------------------------------------*/
  171.  
  172. void 
  173. plD_polyline_os2 (PLStream *pls, short *xa, short *ya, PLINT npts)
  174. {
  175.     PLINT i;
  176.  
  177.     for (i=0; i<npts-1; i++) 
  178.       plD_line_os2( pls, xa[i], ya[i], xa[i+1], ya[i+1] );
  179. }
  180.  
  181. /*----------------------------------------------------------------------*\
  182. *  plD_eop_os2()
  183. *
  184. *  Clear page.
  185. \*----------------------------------------------------------------------*/
  186.  
  187. void    plD_eop_os2( PLStream *pls )
  188. {
  189.     UCHAR c = (UCHAR) CLEAR;
  190.  
  191.     write_command( c, NULL );
  192. }
  193.  
  194. /*----------------------------------------------------------------------*\
  195. *  plD_bop_os2()
  196. *
  197. *  Advance to next page.
  198. \*----------------------------------------------------------------------*/
  199.  
  200. void    plD_bop_os2( PLStream *pls )
  201. {
  202.     UCHAR c = (UCHAR) PAGE;
  203.  
  204.     xold = UNDEFINED;
  205.     yold = UNDEFINED;
  206.  
  207.     write_command( c, NULL );
  208. }
  209.  
  210. /*----------------------------------------------------------------------*\
  211. *  plD_tidy_os2()
  212. *
  213. *  Close graphics file
  214. \*----------------------------------------------------------------------*/
  215.  
  216. void    plD_tidy_os2( PLStream *pls )
  217. {
  218.     UCHAR c = (UCHAR) CLOSE;
  219.     
  220.     write_command( c, NULL );
  221.          
  222.     DosClose( hf );
  223. }
  224.  
  225. /*----------------------------------------------------------------------*\
  226. * plD_state_os2()
  227. *
  228. * Handle change in PLStream state (color, pen width, fill attribute, etc).
  229. \*----------------------------------------------------------------------*/
  230.  
  231. void 
  232. plD_state_os2(PLStream *pls, PLINT op)
  233. {
  234.     write_command( CHANGE_STATE, NULL );
  235.  
  236.     switch (op) {
  237.  
  238.     case PLSTATE_WIDTH:
  239.     write_command( op, &pls->width );
  240.     break;
  241.  
  242.     case PLSTATE_COLOR0:
  243.     write_command( op, &pls->icol0 );
  244.     break;
  245.  
  246.     case PLSTATE_COLOR1:
  247.     break;
  248.     }
  249. }
  250.  
  251. /*----------------------------------------------------------------------*\
  252. *  plD_esc_os2()
  253. *
  254. *  Escape function.  Note that any data written must be in device
  255. *  independent form to maintain the transportability of the metafile.
  256. \*----------------------------------------------------------------------*/
  257.  
  258. void    plD_esc_os2( PLStream *pls, PLINT op, void *ptr )
  259. {
  260.     UCHAR c = (UCHAR) ESCAPE;
  261.     float *color;
  262.     unsigned long ired, igreen, iblue;
  263.     unsigned long    pmrgb;
  264.  
  265.     write_command( c, NULL );
  266.  
  267.     switch (op) {
  268.         case PLESC_SET_RGB: 
  269.         c = (UCHAR) ESC_RGB;
  270.         color = (float *) &ptr[0];
  271.         ired =    min(256,max(0,(int)255.*color[0]));
  272.         igreen= min(256,max(0,(int)255.*color[1]));
  273.         iblue = min(256,max(0,(int)255.*color[2]));
  274.         pmrgb    = (ired   & 0xff) << 16 |
  275.               (igreen & 0xff) <<  8 |
  276.               (iblue  & 0xff);
  277.         write_command( c, &pmrgb );
  278.         //printf( "Changing to RGB value %lx \n", pmrgb );
  279.         break;
  280.     
  281.         default:
  282.         c = (UCHAR) ESC_NOOP;
  283.         write_command( c, NULL );
  284.     }
  285. }
  286.  
  287. /*----------------------------------------------------------------------*\
  288. * Support routines
  289. \*----------------------------------------------------------------------*/
  290.  
  291. /*----------------------------------------------------------------------*\
  292. *    write_command()
  293. *    
  294. *    Function to write a command to the command pipe, and to flush
  295. *    the pipe when full.
  296. \*----------------------------------------------------------------------*/
  297.  
  298. void    write_command( COMMAND_ID cid,    CPARAMS p )
  299. {
  300.     static int i=0;
  301.     static PLINT buffer[ PIPE_BUFFER_SIZE ];
  302.     static PLINT cnt = 0;
  303.     
  304.     i++;
  305.  
  306.     buffer[cnt++] = cid;
  307.     switch( cid ) {
  308.     case LINE:
  309.         buffer[cnt++] = *p++;
  310.         buffer[cnt++] = *p++;
  311.         buffer[cnt++] = *p++;
  312.         buffer[cnt++] = *p++;
  313.         break;
  314.  
  315.     case LINETO:
  316.         buffer[cnt++] = *p++;
  317.         buffer[cnt++] = *p++;
  318.         break;
  319.  
  320.     case NEW_COLOR:
  321.     case NEW_WIDTH:
  322.     case ESC_RGB:
  323.         buffer[cnt++] = *p++;
  324.         break;
  325.  
  326.     case INITIALIZE:
  327.     case CLOSE:
  328.     case SWITCH_TO_TEXT:
  329.     case SWITCH_TO_GRAPH:
  330.     case CLEAR:
  331.     case PAGE:
  332.     case ESC_NOOP:
  333.         break;
  334.     
  335.     case ESCAPE:
  336.         break;
  337.  
  338.     default:
  339.         printf( "Unknown command type--no params passed\n" );
  340.         break;
  341.     }
  342.     if (cnt >= (.9 * PIPE_BUFFER_SIZE) || cid == CLOSE) {
  343.     short rv1, rv2, bytes1, bytes2;
  344.     
  345.     rv1 = DosWrite( hf, &cnt, sizeof( PLINT ), &bytes1 );
  346.     rv2 = DosWrite( hf, buffer, (USHORT) (cnt * sizeof(PLINT)), &bytes2 );
  347.     if (!rv1 && !rv2) 
  348.         /* printf( "%d, %d bytes were written.\n", bytes1, bytes2 ) */ ;
  349.     else 
  350.         printf( "----> write to pipe failed <----\n" );
  351.  
  352.     if (bytes1 != 4 || bytes2 != cnt*sizeof(PLINT) )
  353.         printf( "*** Bogus # of bytes written ***\n" );
  354.  
  355.     cnt=0;
  356.     }
  357. }
  358.